//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.

using UnityEngine;
using System.Collections;

/// <summary>
/// I Script Engine.
/// </summary>
public class IScriptEngine : ScriptEngineBehaviour {					
	static private IScriptEngine instance = null;
	public static IScriptEngine Instance {
		get {	return IScriptEngine.instance;	}
	}					

	/// <summary>
	/// Create a script bind Object.
	/// </summary>
	/// <returns>
	/// The script bind.
	/// </returns>
	/// <param name='scriptBindObj'>
	/// Script bind object.
	/// </param>
	virtual public IScriptBinder CreateScriptBind( GameObject scriptBindObj ) {
		return null; 
	}	
	
	static public void NextMessage(){						
		if( ! m_LockNextMessage ){
			VM vm = VM.Instance;
			if( vm != null ){
				vm.TextProgress();
			}						
		}
	}
	
	void Awake( ){
		if( instance == null){
			instance = this;
			
			Init ();
				
			ViNoDebugger.m_LogEnabled = true;
			
			// Create Default VM with Default Settings.
			VM vm = CreateVM();				
			
			GameObject scriptBindObj = new GameObject( "_ScriptBinder" );
			scriptBindObj.transform.parent = transform;
			scriptBindObj.hideFlags = HideFlags.HideInHierarchy;			
			
			// Create a ScriptBinder.
			m_ScriptBinder = CreateScriptBind( scriptBindObj );
			
			// Attach Script Binder To VM.
			vm.scriptBinder = m_ScriptBinder;		
		}
		else{
			Destroy( this.gameObject );
		}			
	}

	// Protected Functions.	
	protected VM CreateVM(){
		Transform thisTra = transform;
			
		GameObject msgHandObj = new GameObject( "_MessagingHandler" );
		MessagingHandler msgHnd = msgHandObj.AddComponent<MessagingHandler>();
		msgHandObj.transform.parent = thisTra;
				
		GameObject vmObj = new GameObject( "_VM" );
		vmObj.transform.parent = thisTra;				
		VM vm = vmObj.AddComponent<VM>();
		vm.m_MessagingHandler = msgHnd;				
		vm.scriptEngineBehaviour = this;
		
		// Dont Show in Hierarchy these objects.
		msgHandObj.hideFlags = HideFlags.HideInHierarchy;
		vmObj.hideFlags = HideFlags.HideInHierarchy;			
		return vm;
	}

	void OnLevelWasLoaded( int index ){
		if( loadLevelAndClearBackLog ){
			ViNoBackLog.Clear();
		}
	}

	
}
